package com.blunka.framework.client.ui;

import java.util.Map;

import org.jdesktop.swingx.treetable.DefaultMutableTreeTableNode;
import org.jdesktop.swingx.treetable.DefaultTreeTableModel;
import org.jdesktop.swingx.treetable.TreeTableNode;

/**
 * 属性表格面板
 * @author Administrator
 *
 */
public class AttrModel extends DefaultTreeTableModel{

    private String[] _names = {"属性名称", "属性内容"};

	private Class<?>[] _types = {String.class, String.class};

    public AttrModel(TreeTableNode node){
    	super(node);
    }
    /**
     * 列的类型
     */
    @Override
    public Class<?> getColumnClass(int col) {
        return _types[col];
    }

    /**
     * 列的数量
     */
    @Override
    public int getColumnCount() {
        return _names.length;
    }

    /**
     * 表头显示的内容
     */
    @Override
    public String getColumnName(int column) {
        return _names[column];
    }

    /**
     * 返回在单元格中显示的Object
     */
    @Override
    public Object getValueAt(Object node, int column) {
        Object value = null;
        if (node instanceof DefaultMutableTreeTableNode) {
            DefaultMutableTreeTableNode mutableNode = (DefaultMutableTreeTableNode) node;
            Object o = mutableNode.getUserObject();
            if (o != null && o instanceof Map) {
                Map<String,Object> bean = extracted(o);
                switch (column) {
                    case 0:
                        value = bean.get("k");
                        break;
                    case 1:
                        value = bean.get("v");
                        break;
                }
            }
        }
        return value;
    }

	@SuppressWarnings("unchecked")
	private Map<String, Object> extracted(Object o) {
		Map<String, Object> o2 = ((Map<String, Object>)o);
		return o2;
	}

    /**
     * 设置所有单元格都不能编辑
     *
     * @param the node (i.e. row) for which editing is to be determined
     * @param the column for which editing is to be determined
     * @return false
     */
    @Override
    public boolean isCellEditable(Object node, int column) {

    	if(column ==0){
    		return false;
    	}else{
    		return true;
    	}
    }
}
